package com.fulihui.information.biz.content.impl;

import com.fulihui.information.biz.content.ContentBizManager;
import com.fulihui.information.common.config.AppConst;
import com.fulihui.information.core.redis.RedisBasicCategoryContentSortDB;
import com.fulihui.information.core.redis.RedisContentListItemDB;
import com.fulihui.information.core.redis.RedisHotsContentReadTimesSortDB;
import com.fulihui.information.core.redis.RedisHotsContentSharedTimesSortDB;
import com.fulihui.information.dto.ContentListItemDTO;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.fulihui.information.util.ContentScoreUtil.*;
import static org.near.toolkit.common.StringUtil.EMPTY_STRING;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * @author Willard.Hu on 2017/1/12 0012.
 */
@Component
public class ContentBizManagerImpl implements ContentBizManager {
    @Autowired
    private RedisBasicCategoryContentSortDB   redisBasicCategoryContentSortDB;
    @Autowired
    private RedisHotsContentReadTimesSortDB   redisHotsContentReadTimesSortDB;
    @Autowired
    private RedisHotsContentSharedTimesSortDB redisHotsContentSharedTimesSortDB;
    @Autowired
    private RedisContentListItemDB            redisContentListItemDB;

    /**
     * @see ContentBizManager#saveItemsToRedis(Collection)
     */
    @Override
    public void saveItemsToRedis(Collection<ContentListItemDTO> items) {
        if (isEmpty(items)) {
            return;
        }
        // 按类目分组
        Multimap<String, ContentListItemDTO> categoryGroupItems = HashMultimap.create();
        items.forEach(it -> categoryGroupItems.put(it.getCategoryCode(), it));

        // 按组将类目存入Redis
        categoryGroupItems.keySet().forEach(it -> {
            Collection<ContentListItemDTO> list = categoryGroupItems.get(it);
            redisContentListItemDB.batchSave(list);
            Map<String, Double> params = new HashMap<>();
            list.forEach(item -> params.put(item.getId(), categoryScore(item)));
            redisBasicCategoryContentSortDB.bathSave(params, it);
            // 段子不加入到热门中
            if (!isIgnoreCategoryOfHots(it)) {
                // 加入到按阅读数排序的热点数据中
                Map<String, Double> hotsParams = new HashMap<>();
                list.forEach(item -> hotsParams.put(item.getId(), hotsReadTimesScore(item)));
                redisHotsContentReadTimesSortDB.bathSave(hotsParams, EMPTY_STRING);
                // 加入到按分享数排序的热点数据中
                Map<String, Double> hotsParams2 = new HashMap<>();
                list.forEach(item -> hotsParams2.put(item.getId(), hotsSharedTimesScore(item)));
                redisHotsContentSharedTimesSortDB.bathSave(hotsParams2, EMPTY_STRING);
            }
        });
    }

    /**
     * @see ContentBizManager#removeItemsToRedis(Collection)
     */
    @Override
    public void removeItemsToRedis(Collection<ContentListItemDTO> items) {
        if (isEmpty(items)) {
            return;
        }
        // 按类目分组
        Multimap<String, ContentListItemDTO> categoryGroupItems = HashMultimap.create();
        items.forEach(it -> categoryGroupItems.put(it.getCategoryCode(), it));

        // 按组将类目移出Redis
        categoryGroupItems.keySet().forEach(it -> {
            Collection<ContentListItemDTO> list = categoryGroupItems.get(it);
            List<String> ids = list.stream().map(ContentListItemDTO::getId).collect(Collectors.toList());
            String[] idAry = ids.toArray(new String[ids.size()]);
            redisContentListItemDB.batchDelete(idAry);
            redisBasicCategoryContentSortDB.remove(it, idAry);
            // 段子不在热门中
            if (!isIgnoreCategoryOfHots(it)) {
                redisHotsContentReadTimesSortDB.remove(EMPTY_STRING, idAry);
                redisHotsContentSharedTimesSortDB.remove(EMPTY_STRING, idAry);
            }
        });
    }

    /**
     * 是否是被热点过滤的类目
     */
    private boolean isIgnoreCategoryOfHots(String categoryCode) {
        return AppConst.CONTENT_ESSAY_JOKE_CATEGORY_CODE.equals(categoryCode);
    }
}
